package com.netdisk.service.impl;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.management.RuntimeErrorException;

import org.springframework.stereotype.Component;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.netdisk.dao.impl.CapacityDao;
import com.netdisk.dao.impl.FileSystemDao;
import com.netdisk.dao.impl.UserDao;
import com.netdisk.entity.Capacity;
import com.netdisk.entity.Company;
import com.netdisk.entity.FileSystem;
import com.netdisk.entity.Group;
import com.netdisk.entity.Log;
import com.netdisk.entity.User;
import com.netdisk.service.Service;

/**
 * 用户类, 分为单用户,和公司级用户
 * @author chenlong
 *2013-4-13
 */
@org.springframework.stereotype.Service("userService")
public class UserService extends Service<User>{
	
	private UserDao userDao;
	
	private CapacityDao capacityDao;
	
	private FileSystemDao fileSystemDao;
	
	@Resource(name="fileSystemDao")
	public void setFileSystemDao(FileSystemDao fileSystemDao) {
		this.fileSystemDao = fileSystemDao;
	}

	@Resource(name="userDao")
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	@Resource(name="capacityDao")
	public void setCapacityDao(CapacityDao capacityDao) {
		this.capacityDao = capacityDao;
	}
	
	public void save(User user, Log userLog, Capacity cap,Log capLog, FileSystem fileSystem, Log fileLog){
		this.save(user, userLog);
		capacityDao.save(cap);
		logDao.save(capLog);
		
		String storeName = fileSystem.getStoreName();
		File f = new File(storeName);
		f.mkdir();
		String[] temp = storeName.split("/");
		storeName = temp[temp.length - 1];
		fileSystem.setStoreName(storeName);
		fileSystemDao.save(fileSystem);
		logDao.save(fileLog);
	}
	@Override
	public void save(User obj, Log log) {
			userDao.save(obj);
			logDao.save(log);
	}

	@Override
	public void update(User obj, Log log) {
		userDao.update(obj);
		logDao.save(log);
	}

	@Override
	public void delete(User obj, Log log) {
		userDao.delete(obj);
		logDao.save(log);
	}

	@Override
	public List<User> findAll() {
		return userDao.findAll();
	}

	@Override
	public User findById(String id) {
		return userDao.findById(id);
	}

	@Override
	public List<User> findByTop(int from, int max) {
		return userDao.findByTop(from, max);
	}
	
	public User findByLoginName(String loginName) {
		return userDao.findByLoginName(loginName);
	}
	
	public boolean existWithLoginName(String loginName){
		return userDao.existWithLoginName(loginName);
	}

	
}
